<?php
require_once('variables.php');
require_once('functions.php');
mysql_connect("$SQLhost", "$SQLusername", "$SQLpassword") or die("cannot connect"); 
mysql_select_db("$SQLcurrentDatabase")or die("cannot select DB");
$id = mysql_real_escape_string($_POST['id']);
if (! isset($_COOKIE['email']))
{
	echo "You must be logged in to view repertoire.";
	exit(0);
}
$query = "select `title`, `info`, `current` from `song` where `id` = '$id'";
$result = mysql_fetch_array(mysql_query($query));
$title = $result['title'];
$desc = $result['info'];
$current = $result['current'];
$query = "select `id`, `type`, `name`, `target` from `songLink` where `song` = '$id'";
$sql = mysql_query($query);
while ($result = mysql_fetch_array($sql))
{
	$linkIds[] = $result[0];
	$linkTypes[] = $result[1];
	$linkNames[] = $result[2];
	$linkTargets[] = $result[3];
}
$query = "select `typeid`, `name`, `storage` from `mediaType` order by `order` asc";
$sql = mysql_query($query);
while ($result = mysql_fetch_array($sql))
{
	$typeids[] = $result['typeid'];
	$typenames[] = $result['name'];
	$storage[] = $result['storage'];
}
echo "<h2><span id=song_title>$title</span> <span id=repertoire_header style='font-size: 10pt;' data-current='$current'></span></h2><div id=song_desc><pre>$desc</pre></div><br><br>";
$k = 0;
for ($j = 0; $j < count($typeids); $j++)
{
	echo "<div id=\"block_$typeids[$j]\" style=\"margin-top: 16px;\"><h4>$typenames[$j]<span id=\"actions_$typeids[$j]\" class=\"rep_actions\"></span></h4><div class=\"data-storage\" style=\"display: none\">$storage[$j]</div>";
	for ($i = 0; $i < count($linkNames); $i++) if ($linkTypes[$i] == $typeids[$j])
	{
		echo "<div id=\"file_$linkIds[$i]\"><span class=link_actions></span> <span class=\"link_main\">";
		if ($linkNames[$i] == '') echo "<a href=\"about:blank\" style=\"color: red;\">NO NAME</a>";
		else if ($linkTargets[$i] == '') echo "<a href=\"about:blank\" style=\"color: red;\">$linkNames[$i]</a>";
		else echo "<a href=\"$linkTargets[$i]\" target=\"_blank\">$linkNames[$i]</a>";
		echo "</span></div>";
	}
	echo "</div>";
}
?>
